#include "QQEmojiView.h"

QQEmojiStyleProxy::QQEmojiStyleProxy(QStyle* style)
	: QProxyStyle(style)
{
}

void QQEmojiStyleProxy::drawPrimitive(PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
	if (element == PE_Frame)
	{
		return;
	}
	return QProxyStyle::drawPrimitive(element, option, painter, widget);
}

Q_TAKEOVER_NATIVEEVENT_CPP(QQEmojiView, QQEmojiView::mAppBar)
QQEmojiView::QQEmojiView(QWidget* parent)
	: QDialog(parent)
{
	this->setFixedSize(QSize(400, 280));
	QPalette palette = this->palette();
	palette.setColor(QPalette::Window, Qt::white);
	this->setPalette(palette);
	this->setAutoFillBackground(true);

	mAppBar = new ElaAppBar(this);
	mAppBar->setAppBarHeight(0);
	mAppBar->setIsFixedSize(true);

	mToolBar = new ElaToolBar(this);
	mToolBar->addElaIconAction(ElaIconType::IconName::A, tr("Emoji"));
	mToolBar->setIconSize(QSize(30, 30));

	QHBoxLayout* tabLayout = new QHBoxLayout;
	tabLayout->setContentsMargins(0, 5, 0, 5);
	tabLayout->setSpacing(0);
	tabLayout->addWidget(mToolBar);
	tabLayout->addStretch();

	QStackedLayout* stackedLayout = new QStackedLayout;
	stackedLayout->setContentsMargins(0, 0, 0, 0);
	stackedLayout->setSpacing(0);

	QVBoxLayout* layout = new QVBoxLayout;
	layout->setContentsMargins(5, 0, 5, 0);
	layout->setSpacing(0);
	layout->addLayout(stackedLayout);
	layout->addLayout(tabLayout);
	this->setLayout(layout);

	//win api remove title
	/*#ifdef  Q_OS_WIN
		HWND hwnd = reinterpret_cast<HWND>(this->window()->winId());
		::SetWindowLong(hwnd, GWL_STYLE, ::GetWindowLong(hwnd, GWL_STYLE) & ~WS_CAPTION | WS_THICKFRAME);
		::SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
	#endif*/

	connect(mToolBar, &ElaToolBar::actionTriggered, this, &QQEmojiView::doToolBarClick);
}

QQEmojiView::~QQEmojiView()
{
	for (auto style : mStyles)
	{
		if (style)
		{
			delete style;
			style = nullptr;
		}
	}
}

void QQEmojiView::setHaveLoaded(bool loaded)
{
	mHaveLoaded = loaded;
}

bool QQEmojiView::getHaveLoaded() const
{
	return mHaveLoaded;
}

void QQEmojiView::setEmojiSize(QSize size)
{
	QStackedLayout* layout = dynamic_cast<QStackedLayout*>(this->layout()->itemAt(0));
	for (int i = 0; i < layout->count(); i++)
	{
		ElaListView* list = dynamic_cast<ElaListView*>(layout->widget(i));
		dynamic_cast<QQEmojiDelegate*>(list->itemDelegate())->setItemSize(size);
	}
}

void QQEmojiView::appendEmojiOnView(int index, const QQChatEmoji& emoji)
{
	QStackedLayout* layout = dynamic_cast<QStackedLayout*>(this->layout()->itemAt(0));
	ElaListView* list = dynamic_cast<ElaListView*>(layout->widget(index));
	QQEmojiModel* model = dynamic_cast<QQEmojiModel*>(list->model());
	model->addEmoji(emoji);
}

void QQEmojiView::appendEmojiTable(const QList<QQChatEmoji>& emojis)
{
	ElaListView* list = new ElaListView(this);
	QStackedLayout* layout = dynamic_cast<QStackedLayout*>(this->layout()->itemAt(0));
	layout->addWidget(list);
	list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	list->setViewMode(QListView::IconMode);
	list->setFlow(QListView::LeftToRight);
	list->setWrapping(true);
	list->setLayoutMode(QListView::Batched);
	list->setMouseTracking(true);
	list->viewport()->setMouseTracking(true);
	list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
	list->setFocusPolicy(Qt::NoFocus);
	list->setSelectionMode(QAbstractItemView::NoSelection);
	list->setEditTriggers(QAbstractItemView::NoEditTriggers);

	QQEmojiDelegate* delegate = new QQEmojiDelegate(this);
	delegate->setItemSize(QSize(50, 50));
	list->setItemDelegate(delegate);

	QQEmojiModel* model = new QQEmojiModel(this);
	model->addEmojis(emojis);
	mModels.append(model);
	list->setModel(model);
	QQEmojiStyleProxy* style = new QQEmojiStyleProxy(list->style());
	mStyles.append(style);
	list->setStyle(style);
	connect(list, &ElaListView::clicked, this, &QQEmojiView::doEmojiClick);
}

void QQEmojiView::doToolBarClick(QAction* action)
{
	auto actions = mToolBar->actions();
	int index = 0;
	for (int i = 0; i < actions.size(); i++)
	{
		if (actions[i] == action)
		{
			index = i;
			break;
		}
	}
	QStackedLayout* layout = dynamic_cast<QStackedLayout*>(this->layout()->itemAt(0));
	layout->setCurrentIndex(index);
}

void QQEmojiView::doEmojiClick(const QModelIndex& index)
{
	if (index.model() == mModels[0] && index.row() == 0 && index.column() == 0)
	{
		QString filename = QFileDialog::getOpenFileName(this, tr("Select emoji"), QDir::rootPath(), "Image Files (*.png *.jpg *.bmp)");
		emit appendEmoji(filename);
	}
	else
	{
		/*QQChatEmoji data = index.data(Qt::UserRole).toString();
		emit sendEmoji(data, 0);*/
		this->hide();
	}
}
//
//bool QQEmojiView::nativeEvent(const QByteArray& eventType, void* message, long* result)
//{
//	return QDialog::nativeEvent(eventType, message, result);
//}